package com.zj.leetcode.medium.tree;

/**
 * @program: algorithm
 * @description: 完全二叉树的节点个数
 * 222 count-complete-tree-nodes
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class CountCompleteTreeNodes {
    public static void main(String[] args) {
        Solution solution = new CountCompleteTreeNodes().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {

        /**
         * 这是遍历所有了，然后统计下 o(n)
         * 还有一种方式，二分法， o(logN) * o(logN)
         * @param root
         * @return
         */
        public int countNodes(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = countNodes(root.left);
            int right = countNodes(root.right);
            return left + right + 1;

        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
